// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pay_gold: false, //修改信息弹窗
    user_name: '',
    user_picurl: '',
    user_school: '',
    user_faculty: '',
    gold_all_num: 0,
    //修改信息
    inp_school: '',
    inp_faculty: '',

    have_no_gold_read: 1, //是否有未读信息
    have_no_msg_read: 1,
  },

  //跳轉到反饋頁面
  tofeedback:function(){
    wx.navigateTo({
      url: '/pages/feedBack/feedBack',
    })
  },

  //蒙层
  preventTouchMove: function () {},
  //接收信息
  school: function (e) {
    this.setData({
      inp_school: e.detail.value
    })
  },
  faculty: function (e) {
    this.setData({
      inp_faculty: e.detail.value
    })
  },
  //修改信息
  confirm: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    //服务器交互
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/user/change',
      data: {
        user_openid: getApp().globalData.userInfo.user_openid,
        user_name: that.data.user_name,
        user_school: that.data.inp_school,
        user_faculty: that.data.inp_faculty,
        user_picurl: that.data.user_picurl,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.error_code == 0) {
          getApp().globalData.userInfo = res.data.data

          wx.showModal({
            title: '提示！',
            content: '信息修改成功',
            showCancel: false,
            success(res) {
              //用户点击确定后
              that.setData({
                pay_gold: false
              })
              wx.reLaunch({
                url: '/pages/mine/mine'
              })
            }
          })

        } else if (res.data.error_code == 1) {
          wx.showModal({
            title: '提示！',
            content: '学校或学院信息不能为空',
            showCancel: false,
            success(res) {}
          })
        } else if (res.data.error_code == 2) {
          wx.showModal({
            title: '提示！',
            content: '请授权后使用小程序',
            showCancel: false,
            success(res) {}
          })
        } else {
          console.log('mine页面修改信息', res.data)
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀~!',
          content: '网络状态不佳呢！',
          showCancel: false,
          success(res) {}
        })
      },
      complete: function (res) { //注意！
        wx.hideLoading()
      }
    })
  },

  download: function () {
    this.setData({
      pay_gold: true
    })
  },

  cancel: function () {
    this.setData({
      pay_gold: false,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(getApp().globalData.userInfo.user_id==undefined){
      //跳转至授权登录界面
      wx.showModal({
        title: '提示!',
        content: '请授权后再使用小程序',
        showCancel: false,
        success(res) { 
          wx.reLaunch({
            url: '../login/login',
          })
        }
      })
    }else{
      that.setData({ //刷新用户名
        user_name: getApp().globalData.userInfo.user_name,
        user_picurl: getApp().globalData.userInfo.user_picurl,
        user_school: getApp().globalData.userInfo.user_school,
        user_faculty: getApp().globalData.userInfo.user_faculty,
        gold_all_num: getApp().globalData.userInfo.gold_all_num,
      })
    }
  },

  onShow:function(){
    var that = this
    
      wx.request({ //获取金币动态
        url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/mine_load',
        method: "POST", //请求方式,
        header: {
          'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
        },
        data: {
          my_id: getApp().globalData.userInfo.user_id,
        },
        success(res) {
          console.log(res.data)
          that.setData({
            have_no_gold_read: res.data.have_no_gold_read,
            have_no_msg_read:res.data.have_no_msg_read,
            
          })
          console.log(that.data.have_no_gold_read)
          console.log(that.data.have_no_msg_read)
        }
      })

   
  }

})